package com.project.controller.web;

import com.alibaba.fastjson.JSONObject;
import com.project.common.utils.MyPageHelper;
import com.project.entity.*;
import com.project.myBeans.R;
import com.project.myBeans.ResponseBean;
import com.project.service.*;
import com.project.service.base.BaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: TateBrown
 * @date: 2018/5/27 16:18
 * @param:
 * @return:
 */
@EnableAutoConfiguration //自动载入应用程序所需的所有Bean
@RestController //@Controller与@ResponseBody的合并
@RequestMapping(value = "/recruitInfo")
@Api(description = "招聘信息相关",value = "recruitInfo")
public class RecruitInfoController extends BaseService {
    @Autowired
    RecruitInfoService recruitInfoService;

    @Autowired
    private PositionTypeService positionTypeService;

    @Autowired
    private OrgInfoService orgInfoService;

    @Autowired
    private SchoolService schoolService;

    @Autowired
    private ImagesService imagesService;

    @Autowired
    private JobApplyService jobApplyService;
    /**
     * TateBrown 2018年5月27日
     *
     * @param recruitInfo
     * @return
     */
    @ApiOperation(" 批量获取招聘信息 + 条件 + 分页")
    @PostMapping("/findByCondition2")
    public R findByCondition2(@RequestBody RecruitInfo recruitInfo, HttpSession session) {
        User user = (User) session.getAttribute("currentUser");//获取当前用户
        try {
            if (recruitInfo.getPageIndex() > 0) // 存在设置分页
                recruitInfo.setPageIndex(recruitInfo.getPageIndex() - 1);
            List<RecruitInfo> recruitInfos = recruitInfoService.findByCondition(recruitInfo);
            List<Map> maps = new ArrayList<>();
            for (RecruitInfo recruitInfo1 : recruitInfos) {
                Map map = new HashMap();
                PositionType positionType;
                //岗位类型
                if (recruitInfo1.getPositiontype() != null) {
                    positionType = positionTypeService.findById(recruitInfo1.getPositiontype());
                    map.put("positionType", positionType);
                }
                //公司名称
                if (recruitInfo1.getOrgid() != null) {
                    OrgInfo orgInfo = orgInfoService.findByid(recruitInfo1.getOrgid());
                    if (orgInfo != null)
                        map.put("orginfoFullName", orgInfo.getFullname());
                }
                //招聘信息
                map.put("recruitInfo", recruitInfo1);

                //检查学生是否已对此招聘信息发起应聘请求
                JobApply jobApply = new JobApply();
                jobApply.setStid(user.getId());
                jobApply.setRecruitinfoid(recruitInfo1.getId());
                map.put("IsDelivered", jobApplyService.checkIsDeliveredExist(jobApply));
                maps.add(map);
            }
            return R.ok("查询成功").put("data",maps).put("total", MyPageHelper.getTotal(maps));
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("程序异常");
        }
    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param recruitInfo
     * @return
     */
    @ApiOperation("获取招聘信息表信息")
    @PostMapping("/findByCondition")
    public R findByCondition(@RequestBody RecruitInfo recruitInfo) {
        try {
            if (recruitInfo.getPageIndex() > 0) // 存在设置分页
                recruitInfo.setPageIndex(recruitInfo.getPageIndex() - 1);
            return R.ok("查询成功").put("data",recruitInfoService.findByCondition(recruitInfo)).put("total",MyPageHelper.getTotal(recruitInfoService.findByCondition(recruitInfo)));
        } catch (Exception e) {
            e.printStackTrace();
           return R.error("查询失败");
        }
    }

    /**
     * TateBrown 2018年5月27日
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id删除招聘信息")
    @GetMapping("/remove/{id}")
    public R remove(@PathVariable("id") Integer id) {
        try {
            recruitInfoService.remove(id);
            return R.ok("操作成功");
        } catch (Exception e) {
         return R.error("失败");
        }
    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param id
     * @return
     */
    @ApiOperation(" 根据id查找招聘信息 + 学院表 + 图片表")
    @GetMapping("/find/{id}")
    public R find(@PathVariable("id") Integer id) {
        JSONObject jsonObject = new JSONObject();
        try {
            RecruitInfo recruitInfo;
            if (recruitInfoService.findRecruitInfoById(id) != null) {
                recruitInfo = recruitInfoService.findRecruitInfoById(id);
                jsonObject.put("recruitInfo", recruitInfo);
                if (recruitInfo.getSchoolid() != null){
                    School school = schoolService.findOne(recruitInfo.getSchoolid());
                    jsonObject.put("school", school);
                }
                if (recruitInfo.getWorkingpic() != null){
                    Image workingPic = imagesService.findById(recruitInfo.getWorkingpic());
                    workingPic.setPath(configParameters.getServer() + configParameters.getWorkingPicSubPath() + workingPic.getPath());
                    jsonObject.put("workingPic", workingPic);
                }
            }
            return R.ok("操作成功").put("data",jsonObject);
        } catch (Exception e) {
            return R.error("失败");
        }
    }

    /**
     * TateBrown 2018年5月27日
     *
     * @param recruitInfo 招聘信息
     * @return
     */
    @ApiOperation(" 添加招聘信息")
    @PostMapping("/add")
    public R add(@RequestBody RecruitInfo recruitInfo) {
        ResponseBean responseBean = new ResponseBean();
        try {
            recruitInfo.setPublishdate(new java.util.Date());
            recruitInfoService.add(recruitInfo);
            return R.ok("操作成功");
        } catch (Exception e) {
           return R.error("失败");
        }
    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param recruitInfo 招聘信息
     * @return
     */
    @ApiOperation("根据id修改招聘信息")
    @PostMapping("/modify")
    public R modify(@RequestBody RecruitInfo recruitInfo) {
        ResponseBean responseBean = new ResponseBean();
        try {
            recruitInfoService.modifyRecruitInfoById(recruitInfo);
            return R.ok("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
          return R.error("失败");
        }
    }
}
